package com.example.shiwuyouproject.ui.fragment.home.activity

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.lifecycle.Observer
import com.example.shiwuyouproject.R
import com.example.shiwuyouproject.base.BaseBean
import com.example.shiwuyouproject.base.BaseVmActivity
import com.example.shiwuyouproject.databinding.ActivitySearchforcoursesBinding
import com.example.shiwuyouproject.ui.fragment.home.bean.GetSearchListsBean
import com.example.shiwuyouproject.ui.fragment.home.viewmodel.SearchForCoursesViewModel
import com.example.shiwuyouproject.ui.fragment.my.activity.adapter.MyApplicationAdapter
import com.example.shiwuyouproject.ui.fragment.my.activity.bean.MyApplicationBean
import com.example.shiwuyouproject.utils.ActStartUtils
import com.zhy.view.flowlayout.FlowLayout
import com.zhy.view.flowlayout.TagAdapter
import java.util.*


/**
 * 搜索课程
 */
class SearchForCoursesActivity :
    BaseVmActivity<ActivitySearchforcoursesBinding, SearchForCoursesViewModel>() {
    override fun viewModelClass(): Class<SearchForCoursesViewModel> =
        SearchForCoursesViewModel::class.java


    override fun getViewBinding(): ActivitySearchforcoursesBinding {
        return ActivitySearchforcoursesBinding.inflate(layoutInflater)
    }

    var mInflater: LayoutInflater? = null
    override fun initView() {
        mInflater = LayoutInflater.from(this)
        mViewModel.getSearchLists()

    }

    private var myApplicationAdapter: MyApplicationAdapter? = null
    override fun initData() {
        myApplicationAdapter = MyApplicationAdapter()
        mBinding.myRecyclerView.adapter = myApplicationAdapter
        val mview = LayoutInflater.from(this).inflate(R.layout.view_nodata, null)
        myApplicationAdapter?.emptyView = mview
    }

    override fun setListener() {
        mBinding.tvBack.setOnClickListener { finish() }
        mBinding.etInput.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }

            override fun afterTextChanged(s: Editable?) {
                if (mBinding.etInput.text.toString().isEmpty()) {
                    mBinding.myRecyclerView.visibility = View.GONE
                    mBinding.clLab.visibility = View.VISIBLE
                } else {
                    mBinding.myRecyclerView.visibility = View.VISIBLE
                    mBinding.clLab.visibility = View.GONE
                    mViewModel.getCommissionLists(mBinding.etInput.text.toString())
                }
            }
        })
        myApplicationAdapter?.setOnItemClickListener { adapter, view, position ->
            val item: MyApplicationBean? = myApplicationAdapter?.getItem(position)
            val mBundle = Bundle()
            mBundle.putString("post_id", item?.id)
            ActStartUtils.startAct(this, CourseDetailsActivity::class.java, mBundle)
        }
    }

    override fun observe() {
        mViewModel.getSearchListsStatus.observe(this,
            Observer<MutableList<GetSearchListsBean>> {
                mBinding.myTagFlow.adapter = object : TagAdapter<GetSearchListsBean>(it) {
                    override fun getView(
                        parent: FlowLayout,
                        position: Int,
                        item: GetSearchListsBean
                    ): View {
                        val tv = mInflater?.inflate(
                            R.layout.tv,
                            mBinding.myTagFlow, false
                        ) as TextView
                        tv.text = item.keywords
                        return tv
                    }
                }
                mBinding.myTagFlow.setOnSelectListener { myIte ->
                    mBinding.etInput.setText(it[myIte.iterator().next()].keywords)
                    mBinding.clLab.visibility = View.GONE
                }

            })
        mViewModel.getGenRenStatus.observe(this,
            Observer<MutableList<MyApplicationBean>> {
                myApplicationAdapter?.setNewData(it)
            })
    }
}